import OpenAI from 'openai';
import inquirer from 'inquirer';


const openai = new OpenAI({
  apiKey: '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', // defaults to process.env["OPENAI_API_KEY"]
});

async function main() {

  const result = await inquirer.prompt([{
    type: 'input',
    name: "message",
    message: "why?"
  }])

  const stream = await openai.chat.completions.create({
    model: 'gpt-3.5-turbo',
    messages: [{ role: 'user', content: result.message }],
    stream: true,
  });
  for await (const part of stream) {
    process.stdout.write(part.choices[0]?.delta?.content || '');
  }
  process.stdout.write('\n');
  main();
}

main();